package model

import ClickHouse "gin/database"

type ProjectMember struct {
	Id        uint64 `json:"id" gorm:"column:id"`                 // 项目团队用户ID
	ProjectId uint64 `json:"project_id" gorm:"column:project_id"` // 项目ID
	RoleCode  uint64 `json:"role_id" gorm:"column:role_id"`       // 角色code
	UserId    uint64 `json:"user_id" gorm:"column:user_id"`       // 用户ID
	Uuid      string `json:"uuid" gorm:"column:uuid"`             // 全局唯一标识符
}

// 设置表名
func (ProjectMember) TableName() string {
	return "project_member"
}

// GetUserProjectIdListByRoleCode 获得用户特定角色参与的项目
func GetUserProjectIdListByRoleCode(userId uint64, roleCodeList []string, projectCorrespond []string) (res []int64, err error) {
	db := ClickHouse.DB.Model(&ProjectMember{}).Joins("JOIN project p on p.id = project_member.project_id").Where("project_member.user_id = ?", userId)
	if len(roleCodeList) > 0 {
		db = db.Where("project_member.role_code in ?", roleCodeList)
	}
	if len(projectCorrespond) > 0 {
		var statusIdList []uint
		err = ClickHouse.DB.Model(&Status{}).Where("correspond in ?", projectCorrespond).Pluck("id", &statusIdList).Error
		if err != nil || len(statusIdList) == 0 {
			return
		}
		db = db.Where("simpleJSONExtractInt(project.json,'project_status') in ?", statusIdList)
	}
	err = db.Select("project_member.project_id").Group("project_member.project_id").Pluck("project_member.project_id", &res).Error
	return
}
